package com.wu.gumomallmember.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.wu.common.vo.member.MemberUserLoginVo;
import com.wu.common.vo.member.MemberUserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wu.common.entity.member.MemberEntity;
import com.wu.gumomallmember.member.service.MemberService;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.R;


/**
 * ??Ա
 *
 * @author wu
 * @email 2195361340@qq.com
 * @date 2022-12-12 18:25:19
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberUserRegisterVo memberUserRegisterVo) {
        try{
            MemberEntity memberEntity = memberService.register(memberUserRegisterVo);
            return R.ok().setData(memberEntity);
        }catch (RuntimeException e){
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberUserLoginVo memberUserLoginVo) {
        try{
            MemberEntity memberEntity = memberService.login(memberUserLoginVo);
            return R.ok().setData(memberEntity);
        }catch (RuntimeException e){
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
